package 算法题;

/**
 * Created by Administrator on 2017/8/13.
 */
public class 连续子数组的最大值 {
    public static void main(String[] args) {
        int[] arr={6,-3,-2,7,-15,1,2,2};
        int max= FindGreatestSumOfSubArray(arr);
        System.out.println(max);
    }

    private static int FindGreatestSumOfSubArray(int[] arr) {
        if(arr.length<1) return 0;
        int max=arr[0]; //默认连续子数组最大值为第一个元素
        int maxTemp=arr[0];
        for (int i = 0; i < arr.length; i++) {
            maxTemp=(maxTemp<0)?arr[i]:maxTemp+arr[i]; //根据连续子数组和的正负，判断是否需要更新初始子数组
            max=(maxTemp>max)?maxTemp:max;
        }
        return max;
    }
}
